package com.ez.supervise.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
 

/**
 * 
 * @author Searph.wang <br>
 * @Created at Date: 2008-1-10<br>
 * @Email <a href="mailto:wangxiaoning@msn.com">Searph Wang</a>
 */
public class LoginBackInterceptor implements Interceptor {
    protected static final Log log = LogFactory.getLog(LoginBackInterceptor.class);

    private static final long serialVersionUID = 1699208470596101714L;
    private String includeActions;
    private String excludeActions;
    String[] exActionArray;
    String[] inActionArray;

    public void destroy() {
	inActionArray = null;
	exActionArray = null;
    }

    public void init() {
	if (includeActions != null && includeActions.trim().length() > 0) {
	    inActionArray = includeActions.split(";|,");
	}
	if (excludeActions != null && excludeActions.trim().length() > 0) {
	    exActionArray = excludeActions.split(";|,");
	}
    }

    public String intercept(ActionInvocation invocation) throws Exception {
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	HttpSession session = request.getSession();
	String actionName = invocation.getProxy().getActionName();
	String actionNameSpace = invocation.getProxy().getNamespace();
	String actionFullName = actionNameSpace + "/" + actionName + ".action";
	// if (actionNameSpace.contains("admin")) {// 后台管理系统
	if (session.getAttribute(BaseAction.SESSION_USER) != null) {
	    return invocation.invoke();
	} else {
	    // 为 true 应该要求login处理
	    if (!StringUtils.isBlank(actionFullName) && verify(actionFullName)) {
		log.info("filter redirect to: " + BaseAction.getLoginAction() );
		response.sendRedirect(BaseAction.getLoginAction());// 重定向到相应的URL上
		return Action.NONE;// 不用返回
	    }
	    return invocation.invoke();
	}
	// }
    }

    private boolean verify(String actionFullName) {
	for (String url : exActionArray) {
	    if (actionFullName.startsWith(url.trim())) {
		return false;
	    }
	}
	for (String url : inActionArray) {
	    if (actionFullName.startsWith(url.trim())) {
		return true;
	    }
	}
	return false;
    }

    public String getIncludeActions() {
	return includeActions;
    }

    public void setIncludeActions(String includeActions) {
	this.includeActions = includeActions;
	// if (includeActions != null && includeActions.trim().length() > 0) {
	// inActionArray = includeActions.split(";|,");
	// }
    }

    public String getExcludeActions() {
	return excludeActions;
    }

    public void setExcludeActions(String excludeActions) {
	this.excludeActions = excludeActions;
	// if (excludeActions != null && excludeActions.trim().length() > 0) {
	// exActionArray = excludeActions.split(";|,");
	// }
    }

}
